<!DOCTYPE html>
<html lang="zh-cn">
<head>
{include file="public/header" /}
</head>
<body>
<div class="panel admin-panel">
<div class="panel-head"><strong class="icon-menu"> 数据库备份</strong></div>

<form method="post" id="export-form" action="{:url('database/export')}">
<div class="padding border-bottom">
  <ul class="search" style="padding-left:10px;">
    <li> <a class="button border-main " id="export" href="javascript:;"><i class="icon-c-databackup"></i> 立即备份</a> </li>
    <li><a id="optimize" href="{:url('database/optimize')}" class="button border-green"><span class="icon-12001"></span> 优化表</a></li>
    <li><a id="repair" class="button border-green" href="{:url('database/repair')}"><span class="icon-zidongxiufu"></span> 修复表</a></li>
  </ul>
</div>
    <table class="table table-hover text-center">
      <tr>
        <td width="20"><label><input class="check-all" checked="chedked" type="checkbox" value=""></label></td>
        <td width="150" align="center">表名</td>
        <td width="120" align="center">数据量</td>
        <td width="120" align="center">数据大小</td>
        <td width="150" align="center">创建时间</td>
        <td width="150" align="center">说明</td>
        <td width="150" align="center">备份状态</td>
      </tr>
      {volist name="_list" id="table"}
      <tr>
        <td width="20"><input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}"></td>
        <td width="150" align="center">{$table.name}</td>
        <td width="120" align="center">{$table.rows}</td>
        <td width="120" align="center">{$table.data_length|format_bytes}</td>
        <td width="150" align="center">{$table.create_time}</td>
        <td width="150" align="center">{$table.comment}</td>
        <td width="150" align="center" class="info">未备份</td>
      </tr>
      {/volist}
      <tr>
      </tr>
    </table>
  </div>
</form>
<script>
$(function(){
  var $form = $("#export-form"), tables
      $export = $("#export"), 
      $optimize = $("#optimize"), 
      $repair = $("#repair");

      $optimize.add($repair).click(function(){
        $.post(this.href, $form.serialize(), function(data){
          if(data.code){
              layer.msg(data.msg, {icon: 1});
          } else {
              layer.msg(data.msg, {icon: 5});
          }
        }, "json");
        return false;
      });

      $export.click(function(){
        $export.parent().children().addClass("disabled");
        $export.html("<i class='icon-feiji'></i> 正在发送备份请求...");
        $.post(
            $form.attr("action"),
            $form.serialize(),
            function(data){
                if(data.code){
                    tables = data.data.tables;
                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                    backup(data.data.tab);
                    window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                } else {
                    layer.msg(data.msg, {icon: 5});
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            },
            "json"
        );
        return false;
      });

        function backup(tab, code){
            code && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function(data){
                if(data.code){
                    showmsg(tab.id, data.msg);
                    if(!$.isPlainObject(data.data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("<i class='icon-iconfontwancheng'></i> 备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    layer.msg(data.msg, {icon: 5});
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                }
            }, "json");

        }

        function showmsg(id, msg){
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
});
</script>
</body>
</html>